package com.lmqb.controller;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.lmqb.bean.JPhoneSubject;
import com.lmqb.bean.ShowImage;
import com.lmqb.bean.SysConfig;
import com.lmqb.bean.SysUser;
import com.lmqb.constant.FileIO;
import com.lmqb.entry.Config;
import com.lmqb.entry.MenuTitle;
import com.lmqb.entry.PassRate;
import com.lmqb.page.ActivePager;
import com.lmqb.page.SchedulePager;
import com.lmqb.page.ShowImagePager;
import com.lmqb.service.CfgParamsService;
import com.lmqb.service.DataDailyService;
import com.lmqb.service.JiaoYanService;
import com.lmqb.service.ShowImageService;
import com.lmqb.service.SysBaseService;

@Controller
public class SysBaseController extends BaseController{

	@Autowired
	private JiaoYanService jiaoYanService;
	
	@Autowired
	private SysBaseService sysBaseService;
	
	@Autowired
	private DataDailyService dataDailyService;
	
	@Autowired
	private ShowImageService showImageService;
	
	
	@Autowired
	private CfgParamsService cfgParamsService;
	
	//基础配置
	@RequestMapping(value="sysConfig.html",method=RequestMethod.GET)
	public String toSysConfig(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			List<SysConfig> selectAllSysConfig = sysBaseService.selectAllSysConfig();
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("系统配置");
			menuTitle.setMenu("基础配置");
			session.setAttribute(menuTitleF, menuTitle);
			request.setAttribute("SysList", selectAllSysConfig);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "sysConfigList";
	}
	
	//定时任务计划表
	@RequestMapping(value="schedule.html",method=RequestMethod.GET)
	public String toSchedule(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,SchedulePager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(SchedulePager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			SchedulePager schedulePager=sysBaseService.selectSchedulePager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, schedulePager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("系统配置");
			menuTitle.setMenu("定时任务表");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return "scheduleList";
	}
	
	//修改
	@RequestMapping(value="editConfig",method=RequestMethod.POST)
	public String editConfig(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,Config config){
		System.out.println("基础配置修改"+config);
		sysBaseService.updateConfig(config);
		return "redirect:sysConfig.html";
	}
	
	//电话题库
	@RequestMapping(value="phoneList.html",method=RequestMethod.GET)
	public String toPphoneList(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			JPhoneSubject phoneSubject=new JPhoneSubject();
			List<JPhoneSubject> list=sysBaseService.selectAllPhone(phoneSubject);
			request.setAttribute(phoneSubjects, list);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("系统配置");
			menuTitle.setMenu("电话题库");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "phoneList";
	}
	
	
	//数据日报
	@RequestMapping(value="dataDaily.html",method=RequestMethod.GET)
	public String toDataDaily(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			
			/*//获取借款数据日报的数据
			PassRate passRate=sysBaseService.selectPassRate();
			request.setAttribute(passRateF, passRate);
			//获取还款款数据日报的数据
			PassRate rePay = sysBaseService.selectRePay();
			request.setAttribute("repay", rePay);*/
			
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("数据中心");
			menuTitle.setMenu("数据日报");
			session.setAttribute(menuTitleF, menuTitle);
			
			Map<String, Object> map = dataDailyService.selectDataDaily();
			request.setAttribute("map", map);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return "dataToday";
	}
	//催收数据
	@RequestMapping(value="sysCuishou.html",method=RequestMethod.GET)
	public String sysCuishou(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			PassRate passRate=sysBaseService.selectPassRate();
			request.setAttribute(passRateF, passRate);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("数据中心");
			menuTitle.setMenu("催收数据");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return"sysCuishou";
	}
	/**
	 * 审核数据
	 * @param session
	 * @param request
	 * @param response
	 * @param page	当前页
	 * @return
	 */
	@RequestMapping(value="passRate.html",method=RequestMethod.GET)
	public String toPassRate(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,Integer page,Integer pageSize){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("数据中心");
			menuTitle.setMenu("审核数据");
			session.setAttribute(menuTitleF, menuTitle);
			
			Map<String, Object> map = dataDailyService.selectPaseRate(page,pageSize);
			request.setAttribute("map", map);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return "passRate";
	}
	/**
	 * 审核数据导表
	 * @param session
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="excelPassRate",method=RequestMethod.GET)
	public String excelPassRate(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}

			Workbook book = dataDailyService.selectPaseRateToExcel();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("content-disposition",
					"attachment;filename=\"" + UUID.randomUUID().toString().replace("-", "") + ".xlsx" + "\"");
			book.write(response.getOutputStream());
			response.getOutputStream().flush();
			response.flushBuffer();
			response.getOutputStream().close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	  * 百展图展示
	  * @version 1.0 
	  * @author hucong
	  * @date 2017年9月19日
	 */
	@RequestMapping("showImage.html")
	public String showImage(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,ShowImagePager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(ShowImagePager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			ShowImagePager showImagePager = showImageService.selectAll(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, showImagePager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("系统配置");
			menuTitle.setMenu("百展图");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		SysConfig ossPrefix = cfgParamsService.selectOssPrefix();
		request.setAttribute("URL",ossPrefix.getPmValue());
		return "showImage";
	}
	
	/**
	  * 跳转添加百展图页面
	  * @version 1.0 
	  * @author hucong
	  * @date 2017年9月19日
	 */
	@RequestMapping("addShowImage")
	public String addShowImage(){
		
		return "showImageAdd";
	}
	
	/**
	  * 保存添加图片
	  * @version 1.0 
	  * @author hucong
	  * @date 2017年9月19日
	 */
	@RequestMapping(value="saveShoeImage1",method=RequestMethod.POST)
	public String saveShoeImage(ShowImage showImage,MultipartFile file){
		OSSClient ossClient = new OSSClient(FileIO.endpoint, FileIO.accessKeyId, FileIO.accessKeySecret);
		try {
			System.out.println("上传成功");
		    Calendar now = Calendar.getInstance();
		    String key="manage/"+now.get(Calendar.YEAR)+"/"+(now.get(Calendar.MONTH) + 1)+"/"+now.get(Calendar.DAY_OF_MONTH)+"/"+now.getTimeInMillis()+".data";
		    ossClient.putObject(FileIO.bucket, key, file.getInputStream());
		    showImage.setImage(key);
		} catch (Exception e1) {
			System.out.println("上传失败");
			e1.printStackTrace();
		}
		showImageService.insertSelective(showImage);
		
		return "redirect:showImage.html";
	}
	
	/**
	  * 修改状态
	  * @version 1.0 
	  * @author hucong
	  * @date 2017年9月19日
	 */
	@RequestMapping("deleteShowImage")
	public String deleteShowImage(Integer tradId,Byte status){
		ShowImage showImage = new ShowImage(); 
		showImage.setStatus(status);
		showImage.setTradId(tradId);
		showImageService.updateByPrimaryKeySelective(showImage);
		return "redirect:showImage.html";
	}
	
	/**
	  * 修改回显
	  * @version 1.0 
	  * @author hucong
	  * @date 2017年9月19日
	 */
	@RequestMapping("updateShowImage")
	public String updateShowImage(Integer tradId,Map<String, Object> map){
		ShowImage showImage = showImageService.selectBytradId(tradId);
		SysConfig ossPrefix = cfgParamsService.selectOssPrefix();
		map.put("URL", ossPrefix.getPmValue());
		map.put("showImage", showImage);
		return "showImageUpdate";
	}
	/**
	  * 删除当前数据
	  * @version 1.0 
	  * @author hucong
	  * @date 2017年9月20日
	 */
	@RequestMapping("deleteDateShowImage")
	public String deleteDateShowImage(Integer tradId){
		showImageService.deleteDateShowImage(tradId);
		return "redirect:showImage.html";
	}
	
	@RequestMapping(value="modifyShowImage",method=RequestMethod.POST)
	public String modifyShowImage(ShowImage showImage,MultipartFile file){
		if(file.getOriginalFilename().equals("")){
			showImage.setImage(null);
			showImageService.updateByPrimaryKeySelective(showImage);
		}else{
			OSSClient ossClient = new OSSClient(FileIO.endpoint, FileIO.accessKeyId, FileIO.accessKeySecret);
			try {
				System.out.println("上传成功");
			    Calendar now = Calendar.getInstance();
			    String key="manage/"+now.get(Calendar.YEAR)+"/"+(now.get(Calendar.MONTH) + 1)+"/"+now.get(Calendar.DAY_OF_MONTH)+"/"+now.getTimeInMillis()+".data";
			    ossClient.putObject(FileIO.bucket, key, file.getInputStream());
			    showImage.setImage(key);
			} catch (Exception e1) {
				System.out.println("上传失败");
				e1.printStackTrace();
			}
			showImageService.updateByPrimaryKeySelective(showImage);
		}
		
		return "redirect:showImage.html";
	}
	/**
	 * 汇总报表
	 * @param session
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="summaryReport.html",method=RequestMethod.GET)
	public String summaryReport(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("数据中心");
			menuTitle.setMenu("汇总报表");
			session.setAttribute(menuTitleF, menuTitle);
			Map<String, Object> map = dataDailyService.selectSummary();
			request.setAttribute("map", map);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return "summaryReport";
	}
}
